#!/bin/sh
#
# BSD Media Center: Auto configure script (detect and enable drivers to use)
#
# Copyright (c) 2010-2012, The BSD Media Center Development Team 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# PROVIDE: autoconf
# REQUIRE: LOGIN cleanvar
# KEYWORD: nojail
#
# Add the following to /etc/rc.conf[.local] to enable this service
#
# autoconf_enable="YES"
#

. /etc/rc.subr

autoconf_enable=${autoconf_enable-"NO"}

name="autoconf"
rcvar=`set_rcvar`

start_cmd="${name}_start"
stop_cmd=":"

load_rc_config $name
autoconf_msg=${autoconf_msg:-"audio/video allready configured."}

autoconf_start()
{
	# start autconf only if it's the first time
	if [ ! -f /etc/autoconf.done ]; then
		echo "First run: Auto-detecting audio and video drivers..."
		## Audio drivers detection ##
		# Skip if allready loaded sound drivers
		if [ ! -c /dev/mixer0 ]; then
			# Load each snd module one by one (and didn't use the meta driver snd_sound)
			for driver in /boot/kernel/snd_*; do
				driver=$(echo ${driver} | sed 's|/boot/kernel/snd_||')
				if [ ${driver} = "driver.ko" ]; then
					continue;
				fi
				kldload snd_${driver}
				if [ -c /dev/mixer0 ]; then
					# Add the module name to the autoconf list
					echo ${driver} >> /etc/autoconf.done
				fi
				# unload the driver, even if found (case there is more than one audio device)
				kldunload snd_${driver}
			done
			# Because all drivers where unloaded, load them now
			for driver in `grep snd /boot/loader.conf | cut -d "." -f 1`; do
				kldload ${driver}
			done
		fi

		## Video drivers detection ##
		## To do: Need to add the other 2 legacy-type drivers ##
		if ls /root/nvidia-driver*.tbz > /dev/null 2>&1; then
			VIDEO_CARD=`pciconf -l | awk '/vgapci0/ { sub(/chip=0x/,"",$4); print toupper(substr($4,0,4)) }'`
			if grep -q ${VIDEO_CARD} /usr/local/share/nvidia/nvidia.txt; then
				echo "Nvidia video card detected, installing drivers..."
				echo "This step can take about 5 minutes regarding the low speed of USB key"
        		mount -o rw -u /
				#Install nvidia-driver package
				env ASSUME_ALWAYS_YES=true pkg add /root/nvidia-driver*.txz
				#Load driver during boot and disable FreeBSD AGP driver (use nvidia AGP driver)
				echo 'hint.agp.0.disabled="1"' >> /boot/device.hints
				# Configure mplayer for using VDPAU
				if [ -f /root/.mplayer/config ]; then
					echo "vo=vdpau," >> /root/.mplayer/config
					echo "vc=ffh264vdpau,ffmpeg12vdpau,ffodivxvdpau," >> /root/.mplayer/config 
				fi
        		mount -o ro -u /
				echo "nvidia" >> /etc/autoconf.done	
				# Configure Xorg to use dirty nvidia driver
        		echo 'Section "Device"' > /etc/X11/xorg.conf
    			echo ' Identifier  "Card0"' >> /etc/X11/xorg.conf
    			echo ' Driver      "nvidia"' >> /etc/X11/xorg.conf
        		echo 'EndSection' >> /etc/X11/xorg.conf
				echo 'Section "Extensions"' >> /etc/X11/xorg.conf
				echo ' Option "Composite" "Disable"' >> /etc/X11/xorg.conf
				echo 'EndSection' >> /etc/X11/xorg.conf
				# Load the nvidia driver
				kldload nvidia.ko
			fi
		fi
		# Check if a driver was detected
		if [ -f /etc/autoconf.done ]; then
			#Need to populate all kernel modules to load in rc.conf
			echo "#Modules to load (autconf generated)" >> /etc/rc.conf
			backup=$IFS
			IFS="
"
			kmods=""	
			for module in `cat /etc/autoconf.done`; do
				kmods="$kmods $module"
			done
			IFS=$backup
			echo "kld_list=\"$kmods\"" >> /etc/rc.conf
			/usr/local/sbin/config save
       		autoconf_msg="Autoconfiguration of sound and video drivers done: A reboot is advised"
		fi
	fi
	echo $autoconf_msg
}

run_rc_command "$1"
